﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FightTheDragon.GameObjects;
using FightTheDragon.Interface;

namespace FightTheDragon.Managers
{
    class CCamera
    {
        private ObjectManager m_OManager;
        private InterfaceManager m_IManager;
        private InputManager m_InputManager;
        private static CCamera m_Instance = new CCamera();
        private float m_fScroll;
        private float m_fprevScroll;
        private GameTime m_gtTimer;
        private bool m_bScreenShake;

        public static CCamera Instance
        {
            get { return m_Instance; }
        }

        private CCamera()
        {
            m_OManager = ObjectManager.Instance;
            m_IManager = InterfaceManager.Instance;
            m_InputManager = InputManager.Instance;
            m_fScroll = 0;
            m_fprevScroll = 0;
            m_gtTimer = new GameTime();
            m_bScreenShake = false;

        }


        private void StartScreenShake()
        {
            m_bScreenShake = true;
        }

        public void Update(float dt)
        {
            m_fprevScroll = m_fScroll;
            if (m_bScreenShake)
            {
                m_fScroll+= 30;
                if (m_gtTimer.ElapsedGameTime.TotalSeconds > 3)
                {
                    m_bScreenShake = false;
                }

            }

            float Offset = GetInput();
            //Offset should adjust positions
            if (Offset != 0)
            {

                foreach (BaseObject B in m_OManager.Objects)
                {
                    B.VPosition = new Vector2(B.VPosition.X + Offset, B.VPosition.Y);
                }
                foreach (BaseInterfaceObjects B in m_IManager.Objects)
                {
                    B.Position = new Vector2(B.Position.X +Offset, B.Position.Y);
                }
            }

            
            m_InputManager.Update();
        }

       private float GetInput()
        {
            if(m_InputManager.KeyDown(Keys.Left))
            {
                m_fScroll++;
            }
            else if(m_InputManager.KeyDown(Keys.Right))
            {
                m_fScroll--;
            }

            return m_fScroll - m_fprevScroll;
        }

    }
}
